package Contas

class Telefone {
	
	String ddd = ''
	String telefoneParte1 = ''
	String telefoneParte2 = ''
	
	static constraints = {
		ddd(blank:true ,validator: {return isNumb(it) &&  (it.length()) == 2})
		telefoneParte1(blank:true ,validator: {return isNumb(it) &&  (it.length()) == 4})
		telefoneParte2 (blank:true ,validator: {return isNumb(it) &&  (it.length()) == 4})
	}
	
	public static boolean isNumb(String str)
	{
		String s=str;
		for (int i = 0; i < s.length(); i++) {
			//If we find a non-digit character we return false.
			if (!Character.isDigit(s.charAt(i)))
				return false;
		}
		return true;
	}
	
	public String toString(){
		return (ddd + telefoneParte1 + telefoneParte2);		
	}
	
	public String formatFone (){
		return ('('+ddd+') ' + telefoneParte1 + ' - ' + telefoneParte2);
	}
	
	public boolean equals(Telefone telefone){
		return this.toString() == telefone.toString() 
	}

}
